package com.wangp.mywrite.s_netty.nio.ch1;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import static com.wangp.mywrite.s_netty.nio.ch1.ByteBufferUtil.debugAll;


/**
 * @description: TODO
 * @author tooStronger
 * @date 2023/3/15
 */
public class ByteBufferString {

    public static void main(String[] args) {
        ByteBuffer buffer1 = ByteBuffer.allocate(16);
        // 1. 字符串转 buffer
        buffer1.put("hello".getBytes(StandardCharsets.UTF_8));
        debugAll(buffer1);

        // 2. Charset
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");
        debugAll(buffer2);

        // 3. wrap
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8));
        debugAll(buffer3);


        // 还原 （其中 buffer1 还没到读模式，不能直接读）
        String str1 = StandardCharsets.UTF_8.decode(buffer2).toString();
        System.out.println(str1);
    }
}
